package nc.bs.gl.eventlistener.bmcontrol;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.gl.hrpbctprule.constants.GlControlRuleConst;
import nc.bs.gl.hrpbctprule.util.GlControlRuleUtil;
import nc.bs.gl.pubinterface.IVoucherAudit;
import nc.bs.gl.voucher.VoucherBO;
import nc.itf.hrpbm.budgetctrl.service.IBmBudgetControlService;
import nc.pubitf.initgroup.InitGroupQuery;
import nc.vo.arap.bx.util.BXUtil;
import nc.vo.gl.pubinterface.VoucherOperateInterfaceVO;
import nc.vo.gl.pubvoucher.OperationResultVO;
import nc.vo.gl.pubvoucher.VoucherVO;
import nc.vo.pub.BusinessException;

public class BmVoucherAudit implements IVoucherAudit {




    @Override
    public OperationResultVO[] afterAudit(VoucherOperateInterfaceVO vo)
            throws BusinessException {

        boolean isprojControlUsed = (BXUtil.isProductTbbInstalled("M0Z8")
                && InitGroupQuery.isEnabled(InvocationInfoProxy.getInstance().getGroupId(), "M0Z8"));
        if (!isprojControlUsed)
            return null;

//        VoucherBO bo = new VoucherBO();
//        VoucherVO[] vouchers = bo.queryByPks(vo.pk_vouchers);

        VoucherVO[] vouchers = null;

        //ADD BY WW 2025-02-17 提高重算的效率，重算时先从userdata中结束数据
        if (vo.userdata != null && vo.userdata.getClass().isArray()) {
            if (((Object[]) vo.userdata).length == 2 && "VoucherReBusiCtrl".equals(((Object[]) vo.userdata)[0])) {
                vouchers = new VoucherVO[]{(VoucherVO) ((Object[]) vo.userdata)[1]};
            }
        } else {

            VoucherBO bo = new VoucherBO();
            vouchers = bo.queryByPks(vo.pk_vouchers);

        }

        if(vouchers != null && vouchers.length > 0){
            String actionCode = null;
            for(int i = 0; i <vouchers.length; i++){

                if(vouchers[i].getPk_checked() == null || vouchers[i].getPk_checked().equals("")){//取消审核

                    actionCode = "UNAPPROVE";

                }else{//审核

                    actionCode = "APPROVE";

                }
                //判断业务单据配置节点,是否开启了总账控制规则,如果开启,则走新逻辑,否则走旧的业务逻辑
                if (GlControlRuleUtil.enableGl(GlControlRuleConst.BILLTYPE, null)) {
                    //我的新逻辑
                    GlControlRuleUtil.processBudgetControl(actionCode,vouchers[i],
                            null);
                    continue;

                }

                //获取预算控制VO：收入、支出进行分离
                BmCtrlVoucherVOGenerator ctrlvoGenerator = new BmCtrlVoucherVOGenerator();
                AggBmCtrlVoucherVO[] ctrlAggVos = ctrlvoGenerator.generateBmCtrlVO(vouchers[i]);

                /*
                 * 执行预算控制
                 * 先执行收入、再执行支出（支出、资本）
                 */
                String topBudgetCode = null;

                /*
                 * 执行收入预算
                 */
                AggBmCtrlVoucherVO ctrlAggVo_INCOME = ctrlAggVos == null ? null : ctrlAggVos[0];
                topBudgetCode = "YS01";
                //再次处理维度信息
                AggBmCtrlVoucherVO ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_INCOME);

                this.budgetCtrl(topBudgetCode, "收入", actionCode, ctrlAggVo, null);

                /*
                 * 执行资金预算-收入
                 */
                topBudgetCode = "YS05";
                //再次处理维度信息
                ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_INCOME);

                this.budgetCtrl(topBudgetCode, "收入", actionCode, ctrlAggVo, null);

                /*
                 * 执行支出预算
                 */
                AggBmCtrlVoucherVO ctrlAggVo_OUT = ctrlAggVos == null ? null : ctrlAggVos[1];
                topBudgetCode = "YS02";
                //再次处理维度信息
                ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);

                this.budgetCtrl(topBudgetCode, "支出", actionCode, ctrlAggVo, null);


                /*
                 * 执行资本预算
                 */
                topBudgetCode = "YS03";
                //再次处理维度信息
                ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);

                this.budgetCtrl(topBudgetCode, "支出", actionCode, ctrlAggVo, null);

                /*
                 * 执行资金预算-支出
                 */
                topBudgetCode = "YS05";
                //再次处理维度信息
                ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);

                this.budgetCtrl(topBudgetCode, "支出", actionCode, ctrlAggVo, null);

            }

        }


        return null;
    }

    /**
     * 预算控制
     * @param topBudgetCode
     * @param otherFilter
     * @param actionCode
     * @param ctrlAggVo
     * @param oldCtrlAggVo
     * @throws BusinessException
     */
    private void budgetCtrl(String topBudgetCode,String otherFilter,String actionCode,AggBmCtrlVoucherVO ctrlAggVo, AggBmCtrlVoucherVO oldCtrlAggVo) throws BusinessException{

        if(ctrlAggVo != null || oldCtrlAggVo != null){
            NCLocator.getInstance().lookup(IBmBudgetControlService.class).budgetControl(ctrlAggVo.getParentVO().getPk_group(),
                    ctrlAggVo.getParentVO().getPk_org(), "C0", null, otherFilter, actionCode, ctrlAggVo, oldCtrlAggVo, new String[]{topBudgetCode});
        }

    }
    @Override
    public OperationResultVO[] beforeAudit(VoucherOperateInterfaceVO arg0)
            throws BusinessException {
        return null;
    }
}
